import constants as con
from pygame.locals import *

import pygame
import instructionsbg

class Instructions(object):
    def __init__(self,parent):
        self.options = [ 'Start Game', 'Scoreboard', 'Quit' ]
        self.selected = 1
        self.pos = (50,50)

        self.parent = parent
        self.sprites = pygame.sprite.LayeredUpdates()
        self.font = parent.font

        self.sprites = pygame.sprite.LayeredUpdates()
        self.bg = instructionsbg.InstructionsBG(self)


        # here's hoping we're using fixed width
        (self.width,self.height) = self.font.size('A')
        self.line_length = int( (con.window_size[0] - (self.pos[0] * 2)) / self.width)

        self.selected = 1
        self.screen = self.screen1

    def screen2(self):
        pos = self.pos
        str = "Taxtris is a super accurate life simulator. You play the role of dad, who's only source of income is coins that fall from the sky. Your goal is to survive for five minutes without killing your family. Collect money as it falls, and use it to buy things for your family. Try not to let money fall off your platform, or the taxman will get to keep it!"
        pos = self.stringdraw(str,pos)
        pos = (pos[0], pos[1] + (self.height *2))

        str = "The game is played with the mouse. The dad character will chase your mouse, and clicks will either pick up coins or spend coins on needs."
        pos = self.stringdraw(str,pos)
        pos = (pos[0], pos[1] + (self.height *2))

        str = "High scores are achieved by not letting your family die and buying as much stuff for them as you can."
        pos = self.stringdraw(str,pos)
        pos = (pos[0], pos[1] + (self.height *2))

        str = "The game ends either when you run out of time or your family dies. Health needs (with the red cross icon) can save a nearly dead family member."
        pos = self.stringdraw(str,pos)

        self.linedraw("press enter to continue",(50, con.window_size[1] - self.height))

    def screen1(self):
        self.sprites.draw(self.parent.screen)
        self.linedraw("press enter to continue",(50, con.window_size[1] - self.height))

    def screen3(self):
        self.stringdraw("this is screen 3",self.pos)
        
    def redraw(self):

        self.screen()
        
    def linedraw(self,str,pos):
        x = pos[0]
        y = pos[1]

        label = self.font.render(str, True, con.fgcolor)
        self.parent.screen.blit(label, pos)
        y += self.height

        return (x,y)
    
    def stringdraw(self,str,pos):

        x = pos[0]
        y = pos[1]

        buf = ''
        for word in str.split():
            # render if adding this word would put us over the limit
            if (len(buf) + len(word)) >= self.line_length:
                label = self.font.render(buf, True, con.fgcolor)
                self.parent.screen.blit(label, (x,y))
                y += self.height
                buf = word
            elif len(buf) == 0:
                buf = word
            else:
                buf = buf + ' ' + word

        if len(buf):
            label = self.font.render(buf, True, con.fgcolor)
            self.parent.screen.blit(label, (x,y))

        return (x,y)

    def inputDown(self,key):
        if key == K_RETURN:
            self.next_screen()

    def next_screen(self):
        if self.selected == 1:
            print "setting to screen 2"
            self.selected += 1
            self.screen = self.screen2
        elif self.selected == 2:
            print "done, going back to menu"
            self.selected = 1
            self.parent.state = 'Menu'

